import pygame
from settings import *

class MainMenu:
    """主菜单类"""
    def __init__(self):
        # 基本设置
        self.display_surface = pygame.display.get_surface()
        self.font = pygame.font.SysFont('arial', 40)
        
        # 按钮设置
        self.button_width = 200
        self.button_height = 50
        self.button_color = 'White'
        self.button_hover_color = (200, 200, 200)
        
        # 创建按钮
        self.new_game_button = pygame.Rect(
            SCREEN_WIDTH//2 - self.button_width//2,
            SCREEN_HEIGHT//2 - self.button_height//2,
            self.button_width,
            self.button_height
        )
        
        # 加载背景图片
        try:
            self.background = pygame.image.load('./graphics/world/ground.png').convert_alpha()
            # 调整背景图片大小以适应屏幕
            self.background = pygame.transform.scale(self.background, (SCREEN_WIDTH, SCREEN_HEIGHT))
        except:
            print("Warning: Cannot load background image, using solid color")
            self.background = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
            self.background.fill((50, 50, 50))  # 深灰色背景
        
    def draw_text(self, text, color, rect):
        """绘制文本"""
        text_surface = self.font.render(text, True, color)
        text_rect = text_surface.get_rect(center=rect.center)
        self.display_surface.blit(text_surface, text_rect)
        
    def run(self):
        """运行主菜单"""
        mouse_pos = pygame.mouse.get_pos()
        
        # 绘制背景
        self.display_surface.blit(self.background, (0,0))
        
        # 创建半透明遮罩使文字更清晰
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.fill((0,0,0))
        overlay.set_alpha(100)
        self.display_surface.blit(overlay, (0,0))
        
        # 绘制标题
        title_text = self.font.render('My Game', True, 'White')
        title_rect = title_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//3))
        self.display_surface.blit(title_text, title_rect)
        
        # 绘制按钮
        button_color = self.button_hover_color if self.new_game_button.collidepoint(mouse_pos) else self.button_color
        pygame.draw.rect(self.display_surface, button_color, self.new_game_button, border_radius=10)
        self.draw_text('New Game', 'Black', self.new_game_button)
        
        # 更新显示
        pygame.display.update()
        
        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:  # 左键点击
                if self.new_game_button.collidepoint(mouse_pos):
                    print("Starting new game")  # 调试信息
                    return True
        
        return None

class PlayerMenu:
    """暂停菜单类"""
    def __init__(self):
        # 基本设置
        self.display_surface = pygame.display.get_surface()
        self.font = pygame.font.SysFont('arial', 40)
        
        # 按钮设置
        self.button_width = 200
        self.button_height = 50
        self.button_color = 'White'
        self.button_hover_color = (200, 200, 200)
        self.spacing = 20  # 按钮之间的间距
        
        # 创建按钮
        self.buttons = {
            'back': pygame.Rect(
                SCREEN_WIDTH//2 - self.button_width//2,
                SCREEN_HEIGHT//2 - self.button_height*1.5 - self.spacing,
                self.button_width,
                self.button_height
            ),
            'backmain': pygame.Rect(
                SCREEN_WIDTH//2 - self.button_width//2,
                SCREEN_HEIGHT//2 - self.button_height//2,
                self.button_width,
                self.button_height
            ),
            'exit': pygame.Rect(
                SCREEN_WIDTH//2 - self.button_width//2,
                SCREEN_HEIGHT//2 + self.button_height//2 + self.spacing,
                self.button_width,
                self.button_height
            )
        }
        
    def draw_text(self, text, color, rect):
        """绘制文本"""
        text_surface = self.font.render(text, True, color)
        text_rect = text_surface.get_rect(center=rect.center)
        self.display_surface.blit(text_surface, text_rect)
        
    def display(self, events):
        """显示暂停菜单"""
        # 获取当前显示的内容
        current_screen = self.display_surface.copy()
        
        # 创建模糊效果
        scaled_screen = pygame.transform.scale(current_screen, 
                                            (SCREEN_WIDTH//4, SCREEN_HEIGHT//4))
        blurred_screen = pygame.transform.scale(scaled_screen, 
                                              (SCREEN_WIDTH, SCREEN_HEIGHT))
        
        # 创建半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.fill((0,0,0))
        overlay.set_alpha(100)  # 降低透明度，使背景更清晰
        
        # 应用模糊和遮罩
        self.display_surface.blit(blurred_screen, (0,0))
        self.display_surface.blit(overlay, (0,0))
        
        mouse_pos = pygame.mouse.get_pos()
        
        # 绘制所有按钮
        button_texts = {'back': 'Resume Game', 'backmain': 'Main Menu', 'exit': 'Exit Game'}
        
        for button_name, button_rect in self.buttons.items():
            button_color = self.button_hover_color if button_rect.collidepoint(mouse_pos) else self.button_color
            pygame.draw.rect(self.display_surface, button_color, button_rect, border_radius=10)
            self.draw_text(button_texts[button_name], 'Black', button_rect)
            
        # 处理事件
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                for button_name, button_rect in self.buttons.items():
                    if button_rect.collidepoint(mouse_pos):
                        return button_name
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    return 'back'
                    
        return None

class DeathMenu:
    """死亡菜单类"""
    def __init__(self, player):
        # 基本设置
        self.display_surface = pygame.display.get_surface()
        self.player = player
        self.screen_width = self.display_surface.get_width()
        self.screen_height = self.display_surface.get_height()
        
        # 按钮属性
        self.button_width = 200
        self.button_height = 50
        self.button_color = (100, 100, 100)
        self.button_hover_color = (150, 150, 150)
        self.text_color = (255, 255, 255)
        
        # 创建按钮
        self.restart_button = pygame.Rect(
            (self.screen_width - self.button_width) // 2,
            self.screen_height // 2 - 30,
            self.button_width,
            self.button_height
        )
        
        self.quit_button = pygame.Rect(
            (self.screen_width - self.button_width) // 2,
            self.screen_height // 2 + 40,
            self.button_width,
            self.button_height
        )
        
        # 字体
        self.font = pygame.font.Font(None, 36)
        
    def draw(self):
        """绘制死亡菜单"""
        # 绘制半透明背景
        overlay = pygame.Surface((self.screen_width, self.screen_height))
        overlay.fill((0, 0, 0))
        overlay.set_alpha(128)
        self.display_surface.blit(overlay, (0, 0))
        
        # 显示游戏结束和分数
        game_over_text = self.font.render('Game Over', True, self.text_color)
        score_text = self.font.render(f'Final Score: {self.player.score}', True, self.text_color)
        
        game_over_rect = game_over_text.get_rect(center=(self.screen_width // 2, self.screen_height // 2 - 100))
        score_rect = score_text.get_rect(center=(self.screen_width // 2, self.screen_height // 2 - 60))
        
        self.display_surface.blit(game_over_text, game_over_rect)
        self.display_surface.blit(score_text, score_rect)
        
        # 获取鼠标位置
        mouse_pos = pygame.mouse.get_pos()
        
        # 绘制重新开始按钮
        restart_color = self.button_hover_color if self.restart_button.collidepoint(mouse_pos) else self.button_color
        pygame.draw.rect(self.display_surface, restart_color, self.restart_button)
        restart_text = self.font.render('Restart', True, self.text_color)
        restart_text_rect = restart_text.get_rect(center=self.restart_button.center)
        self.display_surface.blit(restart_text, restart_text_rect)
        
        # 绘制退出按钮
        quit_color = self.button_hover_color if self.quit_button.collidepoint(mouse_pos) else self.button_color
        pygame.draw.rect(self.display_surface, quit_color, self.quit_button)
        quit_text = self.font.render('Exit', True, self.text_color)
        quit_text_rect = quit_text.get_rect(center=self.quit_button.center)
        self.display_surface.blit(quit_text, quit_text_rect)
        
    def handle_input(self, event):
        """处理输入事件"""
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:  # 左键点击
            mouse_pos = pygame.mouse.get_pos()
            
            if self.restart_button.collidepoint(mouse_pos):
                # 重置玩家状态
                self.player.health = self.player.max_health
                self.player.mp = self.player.max_mp
                self.player.score = 0
                self.player.show_death_menu = False
                # 重置位置到初始位置
                self.player.pos = pygame.math.Vector2(self.screen_width // 2, self.screen_height // 2)
                return 'restart'
                
            elif self.quit_button.collidepoint(mouse_pos):
                return 'quit'
                
        return None